#include<bits/stdc++.h>
using namespace std;

#define rint register int
const int maxn = 1005;
int n;
int tot, tri[30 * maxn][20];
int ed[maxn * 30];
string s[maxn];

inline int read ()
{
	int x = 1, s = 0;
	char ch = getchar ();
	while (ch < '0' or ch > '9')
	{
		if (ch == '-')
			x = -1;
		ch = getchar ();
	}
	while (ch >= '0' and ch <= '9')
		s = s * 10 + ch - '0', ch = getchar ();
	return x * s;
}

inline int ac (char c)
{
	if (c >= '0' and c <= '9')
		return c - '0' + 1;
	if (c == ':')
		return 11;
	if (c == '.')
		return 12;
}

int num[10], flg[10];
int ck[10];

inline bool chck (int x)
{
	memset (num, 0, sizeof num);
	memset (flg, 0, sizeof flg);
	memset (ck, 0, sizeof ck);
	int nw = 1;
	int len = s[x].size ();
	int t1 = 0, t2 = 0;
	for (rint i (0); i < len; ++i)
	{
		if (s[x][i] == '.')
		{
			if (t2 > 0) return false;
			if (t1 >= 3) return false;
			t1 += 1, nw += 1;
		}
		else if (s[x][i] == ':')
		{
			if (t1 < 3) return false;
			if (t2 >= 1) return false;
			t2 += 1, nw += 1;
		}
		else 
		{
			if (s[x][i] == '0')
			{
				if (flg[nw] != -1) flg[nw] += 1, ck[nw] = 1;
				else num[nw] *= 10, ck[nw] = 1;
			}
			else if (s[x][i] > '0' and s[x][i] <= '9')
			{
				if (flg[nw] > 0) return false;
				flg[nw] = -1;
				num[nw] = num[nw] * 10 + s[x][i] - '0';
				ck[nw] = 1;
			}
		}
	}
	for (rint i (1); i <= 4; ++i)
	{
		if (flg[i] != -1 and flg[i] > 1) 
			return false;
		if (num[i] > 255) 
			return false;
		if (!ck[i])
			return false;
	}
	if (flg[5] != -1 and flg[5] > 1) 
		return false;
	if (num[5] > 65535)
		return false;
	if (!ck[5]) return false;
	return true;
}

inline bool adds (int x)
{
	int rt = 0, nw = ac (s[x][0]);
	for (rint i (0); i < s[x].size (); ++i)
	{
		if (!tri[rt][nw]) tri[rt][nw] = ++tot;
		rt = tri[rt][nw], nw = ac (s[x][i + 1]);
	}
	if (ed[rt])
		return false; 
	ed[rt] = x;
	return true;	
}

inline int fnd (int x)
{
	int rt = 0, nw = ac (s[x][0]);
	for (rint i (0); i < s[x].size (); ++i)
	{
		if (!tri[rt][nw]) return -1;
		rt = tri[rt][nw], nw = ac (s[x][i + 1]);
	}
	if (ed[rt]) 
		return ed[rt];
	return -1;
}

int main ()
{
	freopen ("network.in", "r", stdin); freopen ("network.out", "w", stdout);
	n = read ();
	for (rint i (1); i <= n; ++i)
	{
		char op[10], ad[30];
		scanf ("%s %s", op, ad);
		s[i] = ad;
		if (chck (i) == false)
		{
			printf ("ERR\n");
			continue;
		} 
		if (op[0] == 'S') 
		{
			if (adds (i) == false)
				printf ("FAIL\n");
			else 
				printf ("OK\n");
		}
		else if (op[0] == 'C')
		{
			int tmp = fnd (i);
			if (tmp == -1)
				printf ("FAIL\n");
			else printf ("%d\n", tmp);
		} 
	}
	return 0;
}
